package event;

import java.util.HashMap;

import main.Variable;

import time.Clock;


public class EventQueue {

	private static EventQueue instance;

	private HashMap<Integer,EventList> queue;

	private EventQueue() {
		init();
	}

	public static EventQueue getInstance() {
		if (instance == null) {
			instance = new EventQueue();
		}
		return instance;
	}

	private void init() {
		queue = new HashMap<Integer,EventList>();
	}

	public void handleEvents() {

		while (true) {
			// current time
			int time = Variable.getCurrentTimeStamp();
			if (time > Variable.getEndTimeStamp())
				break;

			// get list at time
			EventList eventList = queue.get(time);
			if (eventList != null) {

				eventList.handleEvents();

				// remove list at time
				queue.remove(time);
			}

			// increate current time
			Variable.increaseCurrentTimeStamp();

		}

		//TODO: end code here
	}

	public void add(int time, Event e) {
		EventList list = queue.get(time);
		if (list == null) {
			list = new EventList();
			queue.put(time, list);
		}
		list.addEvent(e);
	}
	public void add( Event e) {
		int time = e.time;
		EventList list = queue.get(time);
		if (list == null) {
			list = new EventList();
			queue.put(time, list);
		}
		list.addEvent(e);
	}
}


